//uuid
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
    var uuid = s.join("");
    return uuid;
}
var id = uuid()
//cookie
function Cookie() { };
Cookie.prototype.set = function (key, value, expires) {
    var data = new Date();
    data.setSeconds(data.getSeconds() + expires);
    document.cookie = key + '=' + value + ';expires=' + data.toUTCString();
}
Cookie.prototype.get = function (key) {
    var cookie = document.cookie;
    var arr = cookie.split('; ');
    var obj = {};
    arr.forEach(item => {
        var str = item.replace(/=/, '*a*')
        var arr = str.split('*a*')
        obj[arr[0]] = arr[1]
    })
    return obj[key]
}
Cookie.prototype.remove = function (key) {
    this.set(key, '', -1)
}
//注册
$('.register_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
$('.register_verify_img').click(function () {
    id = uuid()
    var uuid_str = "http://127.0.0.1:8000/image_code/?uuid=" + id
    $(this).attr('src', uuid_str)
})
$('.zc_btn').click(function () {
    $.post('http://127.0.0.1:8000/verify_image/', {
        uuid: id,
        code: $('.reg_verify_txt').val().toUpperCase(),
    }, function (data) {
        console.log(data);
        if (data.errmsg == '验证码错误') {
            id = uuid()
            $('.register_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
        }
        if (data.errmsg == '验证码过期') {
            id = uuid()
            $('.register_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
        }
        if (data.errmsg == '验证码通过') {
            $.post('http://127.0.0.1:8000/register/', {
                username: $('.reg_user').val(),
                password: $('.reg_password').val(),
                email: $('.reg_email').val()
            }, function (tips) {
                alert('注册成功')
                location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/login.html'
            })
        }
    })
})
document.addEventListener('keydown', function (e) {
    if (e.key == 'Enter') {
        $.post('http://127.0.0.1:8000/verify_image/', {
        uuid: id,
        code: $('.reg_verify_txt').val().toUpperCase(),
    }, function (data) {
        console.log(data);
        if (data.errmsg == '验证码错误') {
            id = uuid()
            $('.register_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
        }
        if (data.errmsg == '验证码过期') {
            id = uuid()
            $('.register_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
        }
        if (data.errmsg == '验证码通过') {
            $.post('http://127.0.0.1:8000/register/', {
                username: $('.reg_user').val(),
                password: $('.reg_password').val(),
                email: $('.reg_email').val()
            }, function (tips) {
                alert('注册成功')
                location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/login.html'
            })
        }
    })
    }
})
//登录
id = uuid()
$('.login_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
$('.login_verify_img').click(function () {
    id = uuid()
    var uuid_str = "http://127.0.0.1:8000/image_code/?uuid=" + id
    $(this).attr('src', uuid_str)
})
var c = new Cookie()
$('.dl_btn').click(function () {
    $.post('http://127.0.0.1:8000/verify_image/', {
        uuid: id,
        code: $('.login_verify_txt').val().toUpperCase(),
    }, function (data) {
        if (data.errmsg == '验证码错误') {
            id = uuid()
            $('.login_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
        }
        if (data.errmsg == '验证码过期') {
            id = uuid()
            $('.login_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
        }
        if (data.errmsg == '验证码通过') {
            $.post('http://127.0.0.1:8000/login/', {
                username: $('.username').val(),
                password: $('.password').val(),
            }, function (tips) {
                console.log(tips.msg);
                if (tips.msg == '用户名不存在') {
                    alert('用户名不存在')
                    $('.username').val('')
                }
                if (tips.msg == '密码错误') {
                    alert('密码错误')
                    $('.password').val('')
                }
                if (tips.msg == '登录成功') {
                    alert('登录成功')
                    c.set('token', tips.token, 60 * 60 * 24 * 7)
                    location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/index.html'
                }
            })
        }
    })

})
document.addEventListener('keydown', function (e) {
    if (e.key == 'Enter') {
        $.post('http://127.0.0.1:8000/verify_image/', {
            uuid: id,
            code: $('.login_verify_txt').val().toUpperCase(),
        }, function (data) {
            if (data.errmsg == '验证码错误') {
                id = uuid()
                $('.login_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
            }
            if (data.errmsg == '验证码过期') {
                id = uuid()
                $('.login_verify_img').attr('src', "http://127.0.0.1:8000/image_code/?uuid=" + id);
            }
            if (data.errmsg == '验证码通过') {
                $.post('http://127.0.0.1:8000/login/', {
                    username: $('.username').val(),
                    password: $('.password').val(),
                }, function (tips) {
                    console.log(tips.msg);
                    if (tips.msg == '用户名不存在') {
                        alert('用户名不存在')
                        $('.username').val('')
                    }
                    if (tips.msg == '密码错误') {
                        alert('密码错误')
                        $('.password').val('')
                    }
                    if (tips.msg == '登录成功') {
                        alert('登录成功')
                        c.set('token', tips.token, 60 * 60 * 24 * 7)
                        location.href = 'http://127.0.0.1:5500/practise/mzsc_new/html/index.html'
                    }
                })
            }
        })
    }
})